home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland Pascal with Objects 7.0 / PAINT.ZIP / RECT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  2.5 KB  |  89 lines

  1. {************************************************}
  2. {                                                }
  3. {   ObjectWindows Paint demo                     }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. unit Rect;
  9.  
  10. { This unit augments the support for type TRect.
  11. }
  12.  
  13. interface
  14.  
  15. uses WinTypes, WinProcs;
  16.  
  17. type
  18.   RectArray = array[0..3] of TRect;      { To return results from rect ops }
  19.  
  20. { Compute Rect1 - Rect2 as (up to 4) non-overlapping rectangles.
  21.   Returns the number of non-null resulting rectangles.
  22. }
  23. function SubtractRect(var Result: RectArray; var Rect1,
  24.   Rect2: TRect): Integer;
  25.               
  26. implementation
  27.  
  28. { Compute Rect1 - Rect2 as (up to 4) non-overlapping rectangles.
  29.   Returns the number of non-null resulting rectangles.
  30. }
  31. function SubtractRect(var Result: RectArray; var Rect1,
  32.   Rect2: TRect): Integer;
  33.               
  34.   function Max(A, B: Integer): Integer;
  35.   begin
  36.     if A > B then Max := A else Max := B;
  37.   end;
  38.  
  39.   function Min(A, B: Integer): Integer;
  40.   begin
  41.     if A < B then Min := A else Min := B;
  42.   end;
  43.  
  44. var
  45.   I: Integer;
  46. begin
  47.   I := IntersectRect(Result[0], Rect1, Rect2);
  48.   if I = 0 then
  49.   begin
  50.     with Rect1 do
  51.       SetRect(Result[0], Left, Top, Right, Bottom);
  52.     I := 1;                        { difference is Rect1 }
  53.   end
  54.   else
  55.     if not EqualRect(Result[0], Rect1) then  { else difference is empty }
  56.     begin
  57.       I := 0;
  58.       if Rect2.Top > Rect1.Top then        { compute 'top' rectangle }
  59.         with Rect1 do
  60.           begin
  61.             SetRect(Result[I], Left, Top, Right, Rect2.Top);
  62.           Inc(I);
  63.         end;
  64.       if Rect2.Bottom < Rect1.Bottom then  { compute 'bottom' rectangle }
  65.         with Rect1 do
  66.         begin
  67.           SetRect(Result[I], Left, Rect2.Bottom, Right, Bottom);
  68.         Inc(I);
  69.         end;
  70.       if Rect2.Left > Rect1.Left then      { compute 'left' rectangle }
  71.       begin                   { note that top and bottom }
  72.        SetRect(Result[I], Rect1.Left,     { should not overlap }
  73.       Max(Rect1.Top, Rect2.Top), Rect2.Left,
  74.           Min(Rect1.Bottom, Rect2.Bottom));
  75.         Inc(I);
  76.       end;
  77.       if Rect2.Right < Rect1.Right then    { ditto 'right' rectangle }
  78.       begin
  79.         SetRect(Result[I], Rect2.Right, Max(Rect1.Top, Rect2.Top),
  80.           Rect1.Right, Min(Rect1.Bottom, Rect2.Bottom));
  81.         Inc(I);
  82.       end;
  83.   end;
  84.   SubtractRect := I;                 { number of valid rectangles }
  85. end;
  86.  
  87. end.
  88.  
  89.